// pages/detail/detail.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movies: [
      { url: 'https://swcj.majiangyun.com/img/ioc/detail-banner.png' },
      { url: 'https://swcj.majiangyun.com/img/ioc/detail-banner.png' },
      { url: 'https://swcj.majiangyun.com/img/ioc/detail-banner.png' },
      { url: 'https://swcj.majiangyun.com/img/ioc/detail-banner.png' }
    ],
    num: true,
    goods:{},
    article:{},
    list:[],
    coupon: "领取",
    couponList:[],
    specificationList:[],
    userInfo: {},
    curId: 0,
    toView: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (model) {
    var that = this;
    var userInfo = util.getKey("userInfo");
    this.setData({
      userInfo: userInfo
    })
    //商品信息
    that.getModel(model.id);
    that.getList(model.id);
    that.getGoodsCouponList(model.id);
    console.log(that.data.couponList)

  },
  //获取列表
  getModel: function (id) {
    var that = this;
    httpUtil.getHttp("/goods_travel/get/" + id + "?userId=" + that.data.userInfo.id, {}, function (res) {
      if (res.urlList == null || res.urlList == []) {
        res.urlList = [res.url];
      }
      if (res.goodsType == 0) {
        wx.redirectTo({
          url: '../visaInfo/visaInfo?id=' + res.id
        })
      }
      //将markdown内容转换为towxml数据
      let data = app.towxml.toJson(res.text, 'markdown');

      
     
      that.getSpecification(res);
      that.setData({
        goods: res,
        article: data
      })
    })
  },
  getList: function (id) {
    var that = this;
    httpUtil.getHttp("/goods_travel/list?type=4&id=" + id, {}, function (res) {
      that.setData({
        list: res
      })
    })
  }, getGoodsCouponList: function (id) {

    var that = this;
    httpUtil.getHttp("/coupon/get/goods/" + id, {}, function (res) {
      that.setData({
        couponList: res
      })
    })
  }, linkMore: function (e) {
    var id = e.target.dataset.id;
    console.log(id);
    wx.redirectTo ({
      url: '../detail/detail?id=' + id
    })
  }, getSpecification: function (goods) {
    var that = this;
    httpUtil.getHttp("/goods_travel/get/" + goods.id + "/" + goods.goodsType + "/specification", {}, function (res) {
      that.setData({
        specificationList: res,
      })
    })
  }, evalDetail: function (e) {
    var id = e.target.dataset.id;
    var type = e.target.dataset.type;
    wx.navigateTo({
      url: '../evaluate/evaluate?id=' + id + "&type=" + 4
    })
  },
  receiveVou: function (e) {
    var that = this;
    var id = e.target.dataset.id;
    var userInfo = util.getKey("userInfo");
    httpUtil.getHttp("/coupon/add/goods/" + id + "/" + userInfo.id, {}, function (res) {
      if (res.id == '') {
        that.setData({
          coupon: "已领取"
        })
        wx.showToast({
          title: '重复领取',
          icon: 'succes',
          duration: 1000,
          mask: true
        })
      } else if (res.id != '') {
        wx.showToast({
          title: '领取成功',
          icon: 'succes',
          duration: 1000,
          mask: true
        })
        that.setData({
          coupon: "已领取"
        })
      } else {
        wx.showToast({
          title: '领取失败',
          icon: 'succes',
          duration: 1000,
          mask: true
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
 
  },
  purchase:function(){
    var that=this;
    if (that.data.specificationList == null || that.data.specificationList.length==0){
      wx.showToast({
        title: '该商品无可售规格',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
     wx.navigateTo({
      url: '../order/order?id=' +that.data.goods.id
    })
  },

  addShop: function () {
    wx.showToast({
      title: '添加成功',
      icon: 'success',
      duration: 2000
    })
    var that = this;
    that.setData({
      num: false
    })
  },
  showShopCar:function(){
    wx.switchTab({
      url: '../shopCar/shopCar'
    }) 
  },
  clickScroll: function (e) {
    console.log(e);
    var id = e.currentTarget.id
    var item = e.currentTarget.dataset.item
    this.setData({
      toView: id,
      curId: item
    })
  },
})